//
//  ZYMeInfoView.m
//  YiLin
//
//  Created by Zyark on 2018/5/30.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYMeInfoView.h"
@interface ZYMeInfoView()
@property(nonatomic,strong) UIView *infoBgView;
@property(nonatomic,strong) UIImageView *headImageView;
@property(nonatomic,strong) UILabel *nicknameLabel;
@property(nonatomic,strong) UIView *homeModeView;
@property(nonatomic,strong) UIView *leaveHomeModeView;
@property(nonatomic,strong) UILabel *homeModeLabel;
@property(nonatomic,strong) UILabel *leaveHomeModeLabel;
@property(nonatomic,strong) UIImageView *homeModeImageView;
@property(nonatomic,strong) UIImageView *leaveHomeModeImageView;
@end

@implementation ZYMeInfoView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}

-(void)setUp{
    [self setUserInteractionEnabled:YES];
    [ZYUtils setGradualChangingColor:self];//设置背景颜色渐变
    //白色背景
    UIView *infoBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width - 30, self.height * 0.65)];
    [self addSubview:infoBgView];
    infoBgView.backgroundColor = [UIColor whiteColor];
    infoBgView.centerX = self.centerX;
    infoBgView.bottom = self.height - 18;
    infoBgView.layer.cornerRadius = 5;
    infoBgView.layer.masksToBounds = YES;
    
    CGFloat headImageWidth = self.width * 0.2;
    UIImageView *headImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, headImageWidth, headImageWidth)];
    [self addSubview:headImageView];
    headImageView.centerX = self.centerX;
    headImageView.top = infoBgView.top -headImageView.height / 4;
    headImageView.layer.cornerRadius = headImageView.frame.size.width / 2;
    headImageView.layer.masksToBounds = YES;
    
    UILabel *nicknameLabel = [[UILabel alloc] init];
    nicknameLabel.width = self.width;
    nicknameLabel.height = 20;
    [self addSubview:nicknameLabel];
    nicknameLabel.centerX = self.centerX;
    nicknameLabel.top = headImageView.bottom + 5;
    nicknameLabel.textColor = RGBA(102, 102, 102, 1);
    nicknameLabel.font = [UIFont systemFontOfSize:16];
    nicknameLabel.textAlignment = NSTextAlignmentCenter;
    
    _infoBgView = infoBgView;
    _headImageView = headImageView;
    _nicknameLabel = nicknameLabel;
    
    [self setupModeView:ZYModeTypeHome];
    [self setupModeView:ZYModeTypeAwayFromHome];
}

//初始化视图
-(void)setupModeView:(ZYModeType)type{
    UIView *modeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _infoBgView.width / 2, _infoBgView.height / 2)];
    [_infoBgView addSubview:modeView];
    if(type == ZYModeTypeHome){
        //左边
        modeView.left = 0;
    }else{
        modeView.left = _infoBgView.centerY;
    }
    modeView.bottom = _infoBgView.height;
    
    CGFloat width = 45;
    if(ZYiPhone5_OR_5c_OR_5s || ZYiPhone4_OR_4s){
        width = 35;
    }if(ZYiPhone6Plus_OR_6sPlus){
        width = 55;
    }
    UIImageView *modeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width,width)];
    modeImageView.contentMode = UIViewContentModeScaleAspectFill;
    [modeView addSubview:modeImageView];
    if(type == ZYModeTypeHome){
        modeImageView.centerX = modeView.width * 4 / 7;
    }else{
        modeImageView.centerX = modeView.width * 3 / 7 ;
    }
    modeImageView.top = 0;
    UILabel *modeLabel = [[UILabel alloc] init];
    modeLabel.size = CGSizeMake(modeView.width * 0.8, 35);
    [modeView addSubview:modeLabel];
    modeLabel.centerX = modeImageView.centerX;
    modeLabel.top = modeImageView.bottom + 3;
    modeLabel.textColor = RGBA(153, 153, 153, 1);
    modeLabel.font = [UIFont systemFontOfSize:16];
    modeLabel.textAlignment = NSTextAlignmentCenter;
    
    UITapGestureRecognizer *tap;
    
    if(type == ZYModeTypeHome){
        tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(setHomeMode)];
        _homeModeView = modeView;
        _homeModeImageView = modeImageView;
        _homeModeLabel = modeLabel;
    }else{
        tap= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(setLeaveHomeMode)];
        _leaveHomeModeView = modeView;
        _leaveHomeModeImageView = modeImageView;
        _leaveHomeModeLabel = modeLabel;
    }
    [modeImageView setUserInteractionEnabled:YES];
    [modeImageView addGestureRecognizer:tap];
}

#pragma mark - Event
-(void)setHomeMode{
    _modeType = ZYModeTypeHome;
    [_homeModeImageView setImage:[UIImage imageNamed:@"ic_me_info_home_mode_selected"]];
    [_leaveHomeModeImageView setImage:[UIImage imageNamed:@"ic_me_info_leave_home_mode_unselected"]];
    if ([self.delegate respondsToSelector:@selector(meInfoView:setMode:)]) {
        [self.delegate meInfoView:self setMode:_modeType];
    }
}

-(void)setLeaveHomeMode{
    _modeType = ZYModeTypeAwayFromHome;
    [_homeModeImageView setImage:[UIImage imageNamed:@"ic_me_info_home_mode_unselected"]];
    [_leaveHomeModeImageView setImage:[UIImage imageNamed:@"ic_me_info_leave_home_mode_selected"]];
    if ([self.delegate respondsToSelector:@selector(meInfoView:setMode:)]) {
        [self.delegate meInfoView:self setMode:_modeType];
    }
}

#pragma mark - Public
-(void)setModeType:(ZYModeType)modeType{
    if(_modeType == modeType){
        return;
    }
    _modeType = modeType;
    if(modeType == ZYModeTypeHome){
        [self setHomeMode];
    }else{
        [self setLeaveHomeMode];
    }
}

-(void)setHeadUrl:(NSString *)headUrl{
    if(NULLString(headUrl)){
        [_headImageView setImage:[UIImage imageNamed:@"ic_default_head"]];
    }else{
        [_headImageView setImageURL:[NSURL URLWithString:headUrl]];
    }
    
}

-(void)setNickname:(NSString *)nickname{
    _nickname = nickname;
    [_nicknameLabel setText:nickname];
}

-(void)setupTitle{
    ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
    NSString *homeModeString = [language getStringByKey:LC_ME_MAIN_HOME_MODE];
    NSString *leaveHomeModeString = [language getStringByKey:LC_ME_MAIN_LEAVE_HOME_MODE];
    _homeModeLabel.text = homeModeString;
    _leaveHomeModeLabel.text = leaveHomeModeString;
    if([ZYConfig isAtHome]){
        [_homeModeImageView setImage:[UIImage imageNamed:@"ic_me_info_home_mode_selected"]];
        [_leaveHomeModeImageView setImage:[UIImage imageNamed:@"ic_me_info_leave_home_mode_unselected"]];
    }else{
        [_homeModeImageView setImage:[UIImage imageNamed:@"ic_me_info_home_mode_unselected"]];
        [_leaveHomeModeImageView setImage:[UIImage imageNamed:@"ic_me_info_leave_home_mode_selected"]];
    }
}

@end
